package org.edu.modules.jxzy.controller;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.jbsz.entity.VeJwKcxz;
import org.edu.modules.jbsz.service.IVeJwKcxzService;
import org.edu.modules.jxzy.api.CommonApi;
import org.edu.modules.jxzy.api.JwKcxzApi;
import org.edu.modules.jxzy.entity.JwJianzhu;
import org.edu.modules.jxzy.entity.JwJiaoshi;
import org.edu.modules.jxzy.entity.JwKecheng;
import org.edu.modules.jxzy.service.IJwKechengService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.modules.jxzy.utils.JwTextUtils;
import org.edu.modules.scheduleCommon.dto.KechengStatistics;
import org.edu.modules.scheduleCommon.dto.PageDto;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: 教务课程信息
 * @Author: jeecg-boot
 * @Date: 2021-04-15
 * @Version: V1.0
 */
@Api(tags = "教务课程信息")
@RestController
@RequestMapping("/jxzy/jwKecheng")
@Slf4j
public class JwKechengController extends BaseController<JwKecheng, IJwKechengService> {

    @Autowired
    private IJwKechengService jwKechengService;

    @Autowired
    private CommonApi commonApi;

    @Autowired
    private JwKcxzApi kcxzApi;



    @Autowired
    private IVeJwKcxzService veJwKcxzService;


    private String kcmc;

    /**
     * 分页列表查询
     *
     * @param jwKecheng
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "教务课程信息-分页列表查询")
    @ApiOperation(value = "教务课程信息-分页列表查询", notes = "教务课程信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(JwKecheng jwKecheng,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<JwKecheng> queryWrapper = QueryGenerator.initQueryWrapper(jwKecheng, req.getParameterMap());
//        Page<JwKecheng> page = new Page<JwKecheng>(pageNo, pageSize);
//        IPage<JwKecheng> pageList = jwKechengService.page(page, queryWrapper);
        PageDto<JwKecheng> page = new PageDto<>(pageNo, pageSize);
        IPage<JwKecheng> pageList = jwKechengService.pageDto(page, queryWrapper);
        pageList.getRecords().forEach(item ->
        {
            if (item.getKcjstid() != null && item.getKcjstid() != 0) {
                item.setKcjstidText(JwTextUtils.getText(item.getKcjstid()));
            }
        });
        return Result.ok(pageList);
    }

    @ApiOperation("课程管理 导出数据）")
    @PostMapping("/kechengExport")
    public Result<?> kechengExport(JwKecheng jwKecheng, HttpServletResponse resp, HttpServletRequest req, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<JwKecheng> queryWrapper = new LambdaQueryWrapper<>();
        if (oConvertUtils.isNotEmpty(req.getParameter("kcmc"))) {
            queryWrapper.like(JwKecheng::getKcmc, jwKecheng.getKcmc());
        }

        Page<JwKecheng> page = new Page<JwKecheng>(pageNo, pageSize);
        IPage<JwKecheng> pageList = jwKechengService.page(page, queryWrapper);
        pageList.getRecords().forEach(item ->
        {
            if (item.getKcjstid() != null && item.getKcjstid() != 0) {
                item.setKcjstidText(JwTextUtils.getText(item.getKcjstid()));
            }
        });
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("课程号", "kch"));
        list.add(new ExcelPo("课程名称", "kcmc"));
        list.add(new ExcelPo("课程性质", "kcxz_dictText"));

        list.add(new ExcelPo("考试分数", "ksfs"));
        list.add(new ExcelPo("总学分", "xf"));
        list.add(new ExcelPo("实践学时", "sjxs"));
        list.add(new ExcelPo("理论学时", "llxs"));
        list.add(new ExcelPo("其他学时", "qtxs"));
        list.add(new ExcelPo("状态", "status_dictText"));

        PoiUtils.downloadFile(resp, pageList.getRecords(), list);
        return Result.OK();
    }


    @AutoLog(value = "教务课程信息-列表查询")
    @ApiOperation(value = "教务课程信息-列表查询", notes = "教务课程信息-列表查询")
    @GetMapping(value = "/allList")
    public List<JwKecheng> allList(@RequestParam(name = "falid") Integer falid, @RequestParam(name = "specid") Integer specid) {
        JwKecheng jwKecheng = new JwKecheng();
        jwKecheng.setFalid(falid);
        jwKecheng.setSpecid(specid);
        QueryWrapper<JwKecheng> queryWrapper = QueryGenerator.initQueryWrapper(jwKecheng, new HashMap<>());
        return jwKechengService.list(queryWrapper);
    }


    /**
     * 添加
     *
     * @param jwKecheng
     * @return
     */
    @AutoLog(value = "教务课程信息-添加")
    @ApiOperation(value = "教务课程信息-添加", notes = "教务课程信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody JwKecheng jwKecheng) {

        if (StringUtils.isNotEmpty(jwKecheng.getKcjstidText())) {
            jwKecheng.setKcjstid(JwTextUtils.addText(jwKecheng.getKcjstidText()));
        } else {
            jwKecheng.setKcjstid(0);
        }
        jwKechengService.save(jwKecheng);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param jwKecheng
     * @return
     */
    @AutoLog(value = "教务课程信息-编辑")
    @ApiOperation(value = "教务课程信息-编辑", notes = "教务课程信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody JwKecheng jwKecheng) {
        JwKecheng old = jwKechengService.getById(jwKecheng.getId());
        if (StringUtils.isEmpty(jwKecheng.getKcjstidText())) {
            jwKecheng.setKcjstid(0);
        } else if (old.getKcjstid() == null || old.getKcjstid() == 0) {
            jwKecheng.setKcjstid(JwTextUtils.addText(jwKecheng.getKcjstidText()));
        } else {
            JwTextUtils.updateText(old.getKcjstid(), jwKecheng.getKcjstidText());
        }
        jwKechengService.updateById(jwKecheng);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "教务课程信息-通过id删除")
    @ApiOperation(value = "教务课程信息-通过id删除", notes = "教务课程信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        jwKechengService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "教务课程信息-批量删除")
    @ApiOperation(value = "教务课程信息-批量删除", notes = "教务课程信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.jwKechengService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "教务课程信息-通过id查询")
    @ApiOperation(value = "教务课程信息-通过id查询", notes = "教务课程信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        JwKecheng jwKecheng = jwKechengService.getById(id);
        if (jwKecheng == null) {
            return Result.error("未找到对应数据");
        }
        jwKecheng.setKcjstidText(JwTextUtils.getText(jwKecheng.getKcjstid()));
        return Result.ok(jwKecheng);
    }


    /**
     * 通过id获取name
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getNameById")
    public String getNameById(@RequestParam(name = "id", required = true) int id) {
        JwKecheng jwKecheng = jwKechengService.getById(id);
        if (jwKecheng == null) {
            return "未找到对应数据";
        }
        return jwKecheng.getKcmc();
    }

    /**
     * 通过name获取id
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/getIdByName")
    public Integer getIdByName(@RequestParam(name = "name", required = true) String name) {
        LambdaQueryWrapper<JwKecheng> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JwKecheng::getKcmc, name);
        queryWrapper.orderByAsc(JwKecheng::getId);
        queryWrapper.orderByDesc(JwKecheng::getStatus);
        queryWrapper.last(" limit 1");
        JwKecheng jwKecheng = jwKechengService.getOne(queryWrapper);
        if (jwKecheng == null) {
            return null;
        }
        return jwKecheng.getId();
    }

    /**
     * 通过id获取考试分数
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getKsfsById")
    public int getKsfsById(@RequestParam(name = "id", required = true) int id) {
        JwKecheng jwKecheng = jwKechengService.getById(id);
        if (jwKecheng == null) {
            return 0;
        }
        return jwKecheng.getKsfs();
    }


    /**
     * 导出excel
     *
     * @param request
     * @param jwKecheng
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JwKecheng jwKecheng) {
        return super.exportXls(request, jwKecheng, JwKecheng.class, "教务课程信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JwKecheng.class);
    }


    /**
     * 批量启用/禁用
     *
     * @param ids
     * @param status
     * @return
     */
    @AutoLog(value = "教室信息表-批量启用/禁用")
    @ApiOperation(value = "教室信息表-批量启用/禁用", notes = "教室信息表-批量启用/禁用")
    @GetMapping(value = "/updateStatusBatch")
    public Result<?> updateStatusBatch(@RequestParam(name = "ids", required = true) String ids,
                                       @RequestParam(name = "status", required = true) int status) {
        jwKechengService.updateStatusByIds(ids, status);
        return Result.ok("更新状态成功!");
    }

    /**
     * 课程统计接口
     *
     * @param falid
     * @param kcxz
     * @return
     */
    @AutoLog(value = "课程统计")
    @ApiOperation(value = "课程统计", notes = "课程统计")
    @GetMapping("/statistics")
    public Result<?> statistics(@RequestParam(name = "falid", required = false) Integer falid,
                                @RequestParam(name = "kcxz", required = false) Integer kcxz) {
        List<KechengStatistics> kechengStatisticsList = jwKechengService.kechengStatistics(falid, kcxz);
        return Result.OK(kechengStatisticsList);
    }

}
